/*
 * 
 * 
 * 
 */
package com.quhaodian.plug.alidayu;

import com.quhaodian.plug.data.entity.PluginConfig;
import com.quhaodian.plug.data.service.PluginConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * Controller - 支付宝(双接口)
 */
@Controller("aliDayuPlugController")
@RequestMapping("/admin/sendcode_plugin/alidayu")
public class AliDayuPlugController {

    @Resource(name = "dayuPlug")
    private DayuPlug dayuPlug;

    @Resource(name = "pluginConfigServiceImpl")
    private PluginConfigService pluginConfigService;

    /**
     * 安装
     */
    @RequestMapping(value = "/install")
    public String install(RedirectAttributes redirectAttributes) {
        if (!dayuPlug.getIsInstalled()) {
            PluginConfig pluginConfig = new PluginConfig();
            pluginConfig.setPluginId(dayuPlug.getId());
            pluginConfig.setIsEnabled(false);
            pluginConfigService.save(pluginConfig);
        }

        return "redirect:/admin/sendcode_plugin/list.htm";
    }

    /**
     * 卸载
     */
    @RequestMapping(value = "/uninstall")
    public String uninstall(RedirectAttributes redirectAttributes) {
        if (dayuPlug.getIsInstalled()) {
            PluginConfig pluginConfig = dayuPlug.getPluginConfig();
            pluginConfigService.deleteById(pluginConfig.getId());
        }
        return "redirect:/admin/sendcode_plugin/list.htm";
    }

    /**
     * 设置
     */
    @RequestMapping(value = "/setting", method = RequestMethod.GET)
    public String setting(ModelMap model) {
        PluginConfig pluginConfig = dayuPlug.getPluginConfig();
        model.addAttribute("pluginConfig", pluginConfig);
        return "/admin/sendcode_plugin/setting_alidayu";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(String appkey, String secret, String product, String signName, Boolean isEnabled,Integer order) {
        PluginConfig pluginConfig = dayuPlug.getPluginConfig();
        pluginConfig.setAttribute("appkey", appkey);
        pluginConfig.setAttribute("secret", secret);
        pluginConfig.setAttribute("product", product);
        pluginConfig.setAttribute("signName", signName);
        pluginConfig.setIsEnabled(isEnabled);
        pluginConfig.setSortNum(order);
        pluginConfigService.update(pluginConfig);
        //addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/admin/sendcode_plugin/list.htm";
    }

}